package cn.topcodes.tcsf.wechat.web.controller;

import cn.topcode.unicorn.utils.ValidationUtil;
import cn.topcode.unicorn.wxsdk.WXContext;
import cn.topcode.unicorn.wxsdk.base.Result;
import cn.topcode.unicorn.wxsdk.base.WXResponseCode;
import cn.topcode.unicorn.wxsdk.menu.dto.CreateMenu;
import cn.topcode.unicorn.wxsdk.menu.dto.QueryMenuResult;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.wechat.service.WechatMenuService;
import cn.topcodes.tcsf.wechat.service.WechatMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Unicorn
 * @date 2017/6/8
 */
@Controller
@RequestMapping("/admin/wechat/menu")
public class WechatMenuAdminController {

    @Autowired
    private WechatMpService wechatMpService;

    @Autowired
    private WechatMenuService wechatMenuService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index() {
        return "wechat/admin/wechat-menu";
    }

    /*保存*/
    @ResponseBody
    @RequestMapping(value = "save/{wechatMpId}", method = RequestMethod.POST)
    public ApiResponseEntity save(
            @PathVariable("wechatMpId") Long wechatMpId,
            @Valid @RequestBody(required = true) CreateMenu createMenu,
            BindingResult result
    ) {
        ValidationUtil.validate(result);
        wechatMenuService.save(wechatMpId, createMenu);
        return ApiResponseEntity.buildOK();
    }

    /*保存并发布*/
    @ResponseBody
    @RequestMapping(value = "save-and-publish/{wechatMpId}", method = RequestMethod.POST)
    public ApiResponseEntity saveAndPublish(
            @PathVariable("wechatMpId") Long wechatMpId,
            @Valid @RequestBody(required = true) CreateMenu createMenu,
            BindingResult result
    ) {
        ValidationUtil.validate(result);
        wechatMenuService.save(wechatMpId, createMenu);
        String mpId = wechatMpService.getMpId(wechatMpId);
        Result r = WXContext.getMenuInvoker().createMenu(mpId, createMenu);
        if(r != null && r.getErrcode() != null && WXResponseCode.CODE_0_SUCCESS == r.getErrcode()) {
            return ApiResponseEntity.buildOK();
        }else {
            return ApiResponseEntity.buildFailed(r.getErrcode() + ":" + r.getErrmsg());
        }
    }

    @ResponseBody
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public ApiResponseEntity<CreateMenu> get(@RequestParam Long wechatMpId) {
        CreateMenu menu = this.wechatMenuService.getMenu(wechatMpId);
        return ApiResponseEntity.buildOK(menu);
    }

    @ResponseBody
    @RequestMapping(value = "get-from-mp", method = RequestMethod.GET)
    public ApiResponseEntity<CreateMenu> getFromMp(@RequestParam String mpId) {
        QueryMenuResult result = WXContext.getMenuInvoker().queryMenuResult(mpId);
        if(result != null && result.getErrcode() != null &&
                WXResponseCode.CODE_0_SUCCESS == result.getErrcode()) {
            return ApiResponseEntity.buildOK(result.getMenu());
        }
        return ApiResponseEntity.buildFailed(result.getErrcode() + ":" + result.getErrmsg());
    }
}
